package com.ruoyi.project.pay.ali.withdraw;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundTransToaccountTransferModel;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import com.ruoyi.project.pay.ali.modal.AliWithdrawModal;
import com.ruoyi.project.pay.common.PayUrl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;

/**
 * Filename: AliWithdraw.java <br>
 * <p>
 * Description: 支付宝提现<br>
 *
 * @author: rongrong <br>
 * @version: 1.0 <br>
 * @Createtime: 2019-12-09 <br>
 * <p>
 * Modification History:
 * Date         Author          Version            Description
 * ---------------------------------------------------------*
 * 2019-12-09       rongrong            v1.0              第一次创建
 * @Copyright: Copyright (c)2019 by rongrong <br>
 */
@Slf4j
public class AliWithdraw {

    /**
    * @MethodName:
    * @Description:  支付宝提现
    * @param
    * @return
    * @author rongrong
    * @date 2019-12-09
    */
    public static  HashMap<String, Object> tixian(AliWithdrawModal modal) {
        HashMap<String, Object> map = new HashMap<>();

        if(StringUtils.isEmpty(modal.getMoney())){
            map.put("status",500);
            map.put("msg","金额错误");
            return map;
        }
        Integer totalMoney = Integer.valueOf(modal.getMoney())*100;
        /*if(totalMoney < 1){
            map.put("status",500);
            map.put("msg","提现金额小于一元");
            return map;
        }*/
        if(totalMoney > 50000){
            map.put("status",500);
            map.put("msg","每日提现金额不得大于50000");
            return map;
        }
        AlipayClient alipayClient = new DefaultAlipayClient(PayUrl.SERVER_URL, modal.getAppid(),
                modal.getPrivate_key(), PayUrl.FORMAT, PayUrl.CHARSET, modal.getPublice_key(), PayUrl.SIGN_TYPE);
        AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
        AlipayFundTransToaccountTransferModel model = new AlipayFundTransToaccountTransferModel();
        model.setOutBizNo(modal.getOrderId());
        model.setPayeeType("ALIPAY_USERID");
        model.setPayeeAccount(modal.getAliAccount());
        model.setAmount(modal.getMoney());
        model.setPayerShowName(modal.getPayerShowName());
        model.setRemark(modal.getRemark());
        request.setBizModel(model);
        AlipayFundTransToaccountTransferResponse response = null;
        try {
            response = alipayClient.execute(request);
            log.info(response+"");
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if (response.isSuccess() && response.getCode().equals("10000")) {
            map.put("status",200);
            map.put("msg","提现成功");
            return map;
        } else {
            map.put("status",500);
            map.put("msg","提现失败");
            return map;
        }
    }

}
